package com.twetec.cms.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.dao.mapper.system.SystemSitePermissionMapper;
import com.twetec.cms.model.operter.CmsOperter;
import com.twetec.cms.model.po.system.SystemSitePermissionPO;
import com.twetec.cms.model.vo.system.SystemSitePermissionPageListVO;
import com.twetec.cms.model.vo.system.SystemSitePermissionVO;
import com.twetec.cms.service.system.SystemSitePermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2021-03-16
 */
@Service
public class SystemSitePermissionServiceImpl extends ServiceImpl<SystemSitePermissionMapper, SystemSitePermissionPO> implements SystemSitePermissionService {


    @Override
    public SystemSitePermissionPageListVO pageSearchList(String name, String status, Date startTime, Date endTime, List<Long> userIdList, CmsOperter cmsOperter, Integer page, Integer limit) {

        SystemSitePermissionPageListVO systemSitePermissionPageListVO = new SystemSitePermissionPageListVO();
        systemSitePermissionPageListVO.setPage(page);
        systemSitePermissionPageListVO.setLimit(limit);
        systemSitePermissionPageListVO.setTotal(0);
        systemSitePermissionPageListVO.setList(new ArrayList());

        QueryWrapper<SystemSitePermissionPO> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isBlank(name)) queryWrapper.like("SSP.NAME", name);

        if (!StringUtils.isBlank(status)) queryWrapper.eq("SSP.STATUS", status);
        if (null != userIdList && !userIdList.isEmpty()){
            queryWrapper.in("SSP.CREATOR_ID", userIdList);
        }

        // 时间
        if (null != startTime && null != endTime) {
            queryWrapper.between("SSP.gmt_create", startTime, endTime);
        } else if (null != startTime) {
            queryWrapper.gt("SSP.gmt_create", startTime);
        } else if (null != endTime) {
            queryWrapper.lt("SSP.gmt_create", endTime);
        }
        queryWrapper.orderByDesc("SSP.gmt_create");


        Page<SystemSitePermissionPO> systemSitePermissionPOPage = baseMapper.pageSearchList(new Page<SystemSitePermissionPO>(page, limit), queryWrapper);

        systemSitePermissionPageListVO.setTotal((int) systemSitePermissionPOPage.getTotal());
        systemSitePermissionPageListVO.setLimit((int) systemSitePermissionPOPage.getSize());
        systemSitePermissionPageListVO.setPage((int) systemSitePermissionPOPage.getCurrent());

        List<SystemSitePermissionPO> records = systemSitePermissionPOPage.getRecords();
        List<SystemSitePermissionVO> systemSitePermissionVOList = new ArrayList<>();

        //将records集合中的BannerPO对象转换为BannerDTO对象
        for (SystemSitePermissionPO systemSitePermissionPO : records) {
            SystemSitePermissionVO systemSitePermissionVO = new SystemSitePermissionVO();
            BeanUtils.copyProperties(systemSitePermissionPO, systemSitePermissionVO);
            systemSitePermissionVOList.add(systemSitePermissionVO);
        }
        systemSitePermissionPageListVO.setList(systemSitePermissionVOList);

        return systemSitePermissionPageListVO;
    }

    @Override
    public boolean updateStatusById(Long id, String status, CmsOperter cmsOperter) {

        if (null == id || id <= 0){
            return false;
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("STATUS", status);
        updateWrapper.set("MODIFIER_ID", cmsOperter.getId());
        boolean update = super.update(updateWrapper);
        return update;
    }
}
